package jfxtras.scene.control;


import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;


/**
 * 
 * @author Tom Eugelink
 *
 */
public class SpinnerXDataProviderFactory
{
	/**
	 * 
	 * @return
	 */
	static public SpinnerXDataProvider<Integer> integerRange()
	{
		return integerRange(Integer.MIN_VALUE, Integer.MAX_VALUE);
	}
	
	/**
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	static public SpinnerXDataProvider<Integer> integerRange(final int min, final int max)
	{
		return new XSpinnerDataProviderInteger(min, max);
	}
	
	/*
	 * 
	 */
	static class XSpinnerDataProviderInteger extends SpinnerXDataProviderAbstract
	implements SpinnerXDataProvider<Integer>
	{
		public XSpinnerDataProviderInteger(int min, int max)
		{
			super(min, max);
		}
		
		@Override
		public Integer getValue(BigInteger idx)
		{
			return idx.intValue();
		}
		
		@Override
		public BigInteger getIdx(Integer idx)
		{
			return BigInteger.valueOf(idx);
		}
	}

	/**
	 * 
	 * @param list
	 * @return
	 */
	static public <T> SpinnerXDataProvider<T> list(final List<T> list) 
	{
		return new XSpinnerDataProviderList<T>(list);
	}
	
	/*
	 * 
	 */
	static class XSpinnerDataProviderList<T> extends SpinnerXDataProviderAbstract
	implements SpinnerXDataProvider<T>
	{
		public XSpinnerDataProviderList(List<T> list)
		{
			super(0, list.size() - 1);
			this.list = list;
		}
		List<T> list;
		
		@Override
		public T getValue(BigInteger idx)
		{
			return list.get(idx.intValue());
		}
		
		@Override
		public BigInteger getIdx(T value)
		{
			int lIdx = list.indexOf(value);
			return BigInteger.valueOf(lIdx);
		}
	};

	/**
	 * 
	 * @param list
	 * @return
	 */
	static public <T> SpinnerXDataProvider<T> list(final T... list)
	{
		return list(Arrays.asList(list));
	}
}
